import { BoolType, IDataValue, IModuleWithState } from '../types'
import { setDomBoolVal } from '../utils'
import { conSignal } from './conSignal'
import { decoder } from './decoder'

interface ISmState {
    sm_en: BoolType
    sm_value: BoolType
}

interface ISm extends IModuleWithState<ISmState> {
    dataItem: IDataValue<number> | null
}

/**
 * sm模块
 */
export const sm: ISm = {
    name: 'sm',
    dataItem: null,
    state: {
        sm_en: 1,
        sm_value: 0,
    },
    async setState(prop, value) {
        this.state[prop] = value
        setDomBoolVal(prop, this.state[prop] as BoolType)
        if (prop === 'sm_value') {
            // 通知decoder模块，sm_value发生了变化
            decoder.change!()
            // au.setState('au_en', value)
            conSignal.change()
        }
        return
    },
    async neg() {
        // 在时钟下降沿触发时
        if (this.state.sm_en === 1) {
            // 在时钟的下降沿，对自己的value取反
            this.setState('sm_value', this.state.sm_value === 1 ? 0 : 1)
        }
    },
    init() {
        // 设置sm_en的初始值
        setDomBoolVal('sm_en', this.state.sm_en)
        // 设置sm_value的初始值
        setDomBoolVal('sm_value', this.state.sm_value)
    },
}
